<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body>
<div id="pageloading" class="l-loading" style="display:block"></div>
<div class="layui-fluid">
    <!-- breadcrumb -->
    <div class="ad-breadcrumb">
        <div class="layui-breadcrumb" id="crumbs">
        </div>
        <div class="title">查询表格</div>
    </div>

    <!-- search -->
    <div class="ad-search">
        <div id="searchGroup">
        </div>
    </div>

    <!-- main -->
    <div class="layui-card ad-table-card" id="ad-table-card">
        <div class="layui-card-header">
            <div class="layui-table-title">
                <h2>表格查询</h2>
                <div class="layui-table-option layui-form demoTable">
                    <div id="tableDataBtn"></div>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div class="layui-col-md3">
                <div class="ad-table-ztree">
                    <ul id="tree1" class="ztree"></ul>
                </div>
            </div>
            <div class="layui-col-md9">
                <table class="layui-hide" id="table1" lay-filter="tableFilter"></table>
                <script type="text/html" id="barTools">
                    <a class="ad-btn-link" lay-event="edit">编辑</a>
                    <a class="ad-btn-link" lay-event="del">删除</a>
                </script>
            </div>
        </div>

    </div>
</div>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var treeObj = null, tableIns = null;

    var setting = {
        async: {
            enable: true
            ,url: ctxPath + 'systemDept/deptList'
        },
        view: {
            showLine: false,
            showIcon: function(treeId, treeNode) {
                return !treeNode.isParent;
            }
        },
        data: {
            simpleData: {
                enable: true
                ,idKey: "deptId"
                ,pIdKey: "pid"
            }
        },
        callback: {
            onClick: function() {
                refreshTable();
            }
            ,onAsyncSuccess: function () {
                treeObj = $.fn.zTree.getZTreeObj("tree1");
                var node = treeObj.getNodeByParam("level", 0);
                treeObj.expandNode(node);
                treeObj.selectNode(node);
            }
        }
    };

    //刷新表格
    function refreshTable() {
        var node = treeObj.getSelectedNodes()[0];
        tableIns.reload({
            where : {
                pid : node.deptId
            }
            ,page: {
                curr: 1
            }
        });
    }

    ;!function(){
        var table = layui.table
            ,form = layui.form;

        treeObj = $.fn.zTree.init($("#tree1"), setting);

        tableIns = table.render({
            elem: '#table1'
            ,method: 'post'
            ,url: ctxPath + 'systemDept/list'
            , id: 'table1'
            , skin: 'line'
            , cellMinWidth: 100
            , limit: 20
            , where: {
                pid: '1'
            }
            ,cols: [ [
                { type : 'checkbox' },
                { field:'deptCode', title: '部门编号', align : 'center'},
                { field:'name', title: '部门名称', align : 'center'},
                { field:'prefix', title: '前缀代码', align : 'center'},
                { field:'contact', title: '联系人', align : 'center'},
                { field:'tel', title: '联系电话', align : 'center'},
                { field : '', title : '操作', width : 150, align : 'center', toolbar : '#barTools', fixed: 'right' }
            ] ]
            ,response: {
                statusName: 'searchCount'
                ,statusCode: true
                ,countName: 'total'
                ,dataName: 'records'
            }
            ,page: true
        });

        // 监听操作列
        table.on('tool(tableFilter)', function(obj) {
            if (obj.event == 'edit') {
                do_edit(obj);
            } else if (obj.event == 'del') {
                do_delete(obj);
            }
        });

        //表格操作项按钮调用
        $("#tableDataBtn").tableDataBtn({
            data:[
                {id:'add', name:'新增', icon:'&#xe654;', dataType:'add'}
            ],
            batchData:[
                {id:'batch_del', name:'批量删除', icon:'&#xe640;', dataType:'batch_del'}
            ]
        })
        var active = {
            add: do_add
            ,batch_del: function(){
                var checkStatus = table.checkStatus('table1');
                do_batch_delete(checkStatus.data);
            }
        };
        $.actionFun(tableIns, active, form);

        //filter
        var filterJson = [
            {"text":"部门名称", "value": "name", "type":'input'}
        ]

        //查询
        $("#searchGroup").searchGroup({
            data: filterJson, //输入框的数据
            placeholder: true, //是否显示输入前提示文字
            //rows: 4 //每行显示个数
        });

        //查询表格
        form.on('submit(filterForm)', function(data) {
            var data = data.field;
            tableIns.reload({
                where : data
                ,page: {
                    curr: 1
                }
            });
            return false;
        });
    }();

    function do_add() {
        var node = treeObj.getSelectedNodes()[0];
        // if (node) {
        //     if (node.level > 2) {
        //         layer.msg('只能新增二/三级部门，请选择正确的上级部门！', {
        //             icon : 2,
        //             anim : 6
        //         });
        //         return;
        //     }
        // }
        var url = ctxPath + "systemDept/add?pid=" + node.deptId;
        var btns = [ '保存', '关闭' ];
        var btnFns = {
            yes : function(index, layero) {
                layero.find("iframe").contents().find("form [lay-submit]").click();
            }
        };
        openWin(850, 550, '部门信息-新增', url, btns, btnFns);
    }

    function do_edit(obj) {
        var url = ctxPath + "systemDept/edit?deptId=" + obj.data.deptId;
        var btns = [ '保存', '关闭' ];
        var btnFns = {
            yes : function(index, layero) {
                layero.find("iframe").contents().find("form [lay-submit]").click();
            }
        };
        openWin(850, 550, '部门信息-修改', url, btns, btnFns);
    }

    function do_delete(obj) {
        var deptId = obj.data.deptId;
        if (deptId == "1") {
            layer.msg("不能删除根部门！", {
                icon : 2,
                anim : 6
            });
            return;
        } else {
            layer.confirm('真的要删除选中的部门及其子部门吗?',
                {icon: 3}
                , function (index) {
                    layer.close(index);
                    $.ajax({
                        type: 'post',
                        url: ctxPath + "systemDept/delete",
                        data: {
                            deptIds: deptId
                        },
                        success: function (result) {
                            if (result) {
                                layer.msg("删除成功！", {
                                    icon: 1
                                    , time: 1000
                                }, function () {
                                    var node = treeObj.getNodeByParam("deptId", deptId);
                                    treeObj.removeNode(node);
                                    tableIns.reload();
                                    $(".layui-table-alert").find("em").html(0);
                                });
                            }
                        }
                    });
                }
            );
        }
    }

    function do_batch_delete(obj) {
        if (obj.length == 0) {
            layer.msg('请选择至少一项');
            return false;
        }
        var pks = [];
        $.each(obj, function (index, data) {
            pks.push(data.deptId);
        });
        //后台删除数据
        if (($.inArray("1", pks) != -1)) {
            layer.msg("不能删除根部门！", {
                icon : 2,
                anim : 6
            });
            return;
        } else {
            layer.confirm("确定要删除选中的部门及其子部门吗？", {
                icon : 3
            }, function(index) {
                layer.close(index);
                $.ajax({
                    type: 'post',
                    url : ctxPath + "systemDept/delete",
                    data : {
                        deptIds : pks.join(",")
                    },
                    success : function(result) {
                        if (result) {
                            layer.msg("删除成功！", {
                                icon: 1
                                , time: 1000
                            }, function () {
                                $.each(pks, function (index, data) {
                                    var node = treeObj.getNodeByParam("deptId", data);
                                    treeObj.removeNode(node);
                                });
                                tableIns.reload();
                                $(".layui-table-alert").find("em").html(0);
                            });
                        }
                    }
                });
            });
        }
    }

</script>